<%@ taglib uri="/WEB-INF/tlds/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu.tld" prefix="menu" %>
<%@ taglib uri="/WEB-INF/tlds/struts-menu-el.tld" prefix="menu-el" %>
<%@ taglib uri="/WEB-INF/tlds/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tlds/fmt.tld" prefix="fmt" %>
<%@ taglib uri="/WEB-INF/tlds/authz.tld" prefix="authz" %>
<%@ taglib uri="/WEB-INF/tlds/sitemesh-decorator.tld" prefix="decorator"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" scope="request"/>
<script type="text/javascript" src="${ctx}/dwr/interface/ConfigSaverDWR.js"></script>
<script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
<script type="text/javascript" src="${ctx}/dwr/util.js"></script>
<script type="text/javascript">
	function saveConnexion() {
			$('method').value='modifyCon';
			$('applicationForm').submit();		
	}
	
	function importConfig() {
		var importFile=$('file').value;
		if(!importFile || importFile.length==0) {
			alert('<bean:message key="info.selectImport"/>');
		} else if(importFile.indexOf('.xml')!=(importFile.length-4)) {
			alert('<bean:message key="info.importExtension"/>');
		} else {
			$("uploadTried").value="true";
			$("uploadForm").submit();
		}
	}
	
	
	function saveAndExportConfig(isModified){
		// AJAX call
		var id = $F("id");
		if(isModified && confirm('<bean:message key="info.confirmExport"/>')){
			ConfigSaverDWR.saveApplicationConfig(exportConfig,id);
		}else{
			exportConfig(false);
		}
	}	
	
	function exportConfig(ret){
		if(ret){
			this.self.location = '${ctx}/application.do?method=edit';			
		}
		var width='800';
		var height='600';
		var wnd2 = window.open('${ctx}/application.do?method=exportCfg','exportWnd','scrollbars=yes,toolbar=yes,menubar=yes,resizable=yes,width='+width+',height='+height+',left=200');
		wnd2.focus();
	}
	
	function saveConfig() {
		// AJAX call
		var id = $F("id");
		ConfigSaverDWR.saveApplicationConfig(message,id);
	}					
	function message(ret) {
		var id = $F("id");
		if(ret) {
			alert('<bean:message key="info.saveDone"/>');
			this.self.location = '${ctx}/application.do?method=loadAppli&id='+id;
		} else {
			alert('<bean:message key="info.saveNotDone"/>');
		}
	}
	function prevCon(){
			$('previousConnexion').value=$F("application.connectionName");	
	}
	
	function initRetUpload(){
		$("retUpload").value="false";
		$("uploadTried").value="false";
	}
</script>
<div id="tabPanelWrapper">
	<div id="tabPanel">
		<ul>
			<li><a id="currentTab" href="#" onclick=""><bean:message key="application.generalTitle" /></a></li>
			<li><a href="application.do?method=list"onclick="initRetUpload();"><bean:message key="application.resourceTitle" /></a></li>
			<li><a href="variable.do?method=list" onclick="initRetUpload();"><bean:message key="application.variableTitle" /></a></li>
		</ul>
	</div>
	<div id="tabContent">
		<h1><bean:write name="currentApplication" property="name" /></h1>
		<div id="subcontent">
		<html:form styleId="applicationForm" action="/application" focus="conList" >
				<input type="hidden" name="method" value="" id="method" />
				<input type="hidden" name="forward" value="general" />
				<html:hidden styleId="id" property="application.id" />
				<html:hidden styleId="name" property="application.name" />
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">				
				<p><bean:message key="generalPage.connection" /></p>
				<p>				
					<html:select name="applicationForm" property="application.connection.name" styleId="conList">
						<html:option value="">&nbsp; </html:option>
						<html:options collection="connections" property="name" />
					</html:select>		
				</p>
				
				<p><input name="validateConButton" type="submit" value="<bean:message key='generalPage.validate' />" onclick="saveConnexion(); return false;"/>
				&nbsp;<input name="cancelPoolButton" type="reset" value="<bean:message key='generalPage.cancel' />"/></p>
</authz:authorize>
<authz:authorize ifAllGranted="ROLE_USER">
				<p><bean:message key="generalPage.connection" />						
					&nbsp;:&nbsp;<bean:write name="applicationForm" property="application.connectionName" /></p>
					<html:hidden name="applicationForm" property="application.connectionName" styleId="conList"/>
</authz:authorize>

			<br />
		</html:form>			
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">
			<html:form styleId="uploadForm" action="/uploadFile" method="post" enctype="multipart/form-data">
				<input type="hidden" name="id" value="<bean:write property='application.id' name='applicationForm'/>"/>
				<input type="hidden" name="retUpload" value="${retUpload}"/>
				<html:hidden  name="uploadForm" property="uploadTried" styleId="uploadTried"/>
				<p><bean:message key="generalPage.import"/></p>
				<p><html:file property="file" maxlength="251" size="50" styleId="file"/></p>
				
				<p>
					<input name="importButton" type="submit" value="<bean:message key='generalPage.importButton' />"
			onclick="importConfig();return false;" /></p>
			</html:form>
			<br />			
			<p><bean:message key="generalPage.export"/></p>			
			<p><input name="" type="submit" onclick="saveAndExportConfig(${isApplicationModified});" value="<bean:message key='generalPage.exportButton' />" /></p>
</authz:authorize>
<authz:authorize ifAllGranted="ROLE_USER">
				<br />			
				<p><bean:message key="generalPage.export"/></p>			
				<p><input name="" type="submit" onclick="exportConfig();" value="<bean:message key='generalPage.exportButton' />" /></p>				
</authz:authorize>			
		</div>
		<p>
<authz:authorize ifAllGranted="ROLE_SUPERVISOR">								
			<input type="submit" value="<bean:message key='allPage.saveConfig' />" onclick="saveConfig();return false;"/>
</authz:authorize>

		</p>
	</div>
</div>
<script type="text/javascript">
		var retUpload = $F("retUpload");
		var uploadTried = $F("uploadTried");
		if(uploadTried=="true"){
			if(retUpload=="true"){
				alert('<bean:message key="info.importOK"/>');	
				$("retUpload").value="false";
				$("uploadTried").value="false";
			}else {
				alert('<bean:message key="info.importNotDone"/>');			
			}
		}
</script>